package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wave.common.utils.StringUtils;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.wave.system.domain.bo.SysMechanicalArmInfoBo;
import com.wave.system.domain.vo.SysMechanicalArmInfoVo;
import com.wave.system.domain.SysMechanicalArmInfo;
import com.wave.system.mapper.SysMechanicalArmInfoMapper;
import com.wave.system.service.ISysMechanicalArmInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 机械臂信息Service业务层处理
 *
 * @author wave
 * @date 2024-10-15
 */
@RequiredArgsConstructor
@Service
public class SysMechanicalArmInfoServiceImpl implements ISysMechanicalArmInfoService {

    private final SysMechanicalArmInfoMapper baseMapper;

    /**
     * 查询机械臂信息
     */
    @Override
    public SysMechanicalArmInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机械臂信息列表
     */
    @Override
    public TableDataInfo<SysMechanicalArmInfoVo> queryPageList(SysMechanicalArmInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMechanicalArmInfo> lqw = buildQueryWrapper(bo);
        Page<SysMechanicalArmInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询机械臂信息列表
     */
    @Override
    public List<SysMechanicalArmInfoVo> queryList(SysMechanicalArmInfoBo bo) {
        LambdaQueryWrapper<SysMechanicalArmInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMechanicalArmInfo> buildQueryWrapper(SysMechanicalArmInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMechanicalArmInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getArmNumber()), SysMechanicalArmInfo::getArmNumber, bo.getArmNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getConnectMode()), SysMechanicalArmInfo::getConnectMode, bo.getConnectMode());
        lqw.eq(StringUtils.isNotBlank(bo.getRunningState()), SysMechanicalArmInfo::getRunningState, bo.getRunningState());
        lqw.eq(StringUtils.isNotBlank(bo.getArmTaskState()), SysMechanicalArmInfo::getArmTaskState, bo.getArmTaskState());
        lqw.eq(StringUtils.isNotBlank(bo.getWcsTaskState()), SysMechanicalArmInfo::getWcsTaskState, bo.getWcsTaskState());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCode()), SysMechanicalArmInfo::getFaultCode, bo.getFaultCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPickupLocation()), SysMechanicalArmInfo::getPickupLocation, bo.getPickupLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryLocation()), SysMechanicalArmInfo::getDeliveryLocation, bo.getDeliveryLocation());
        return lqw;
    }

    /**
     * 新增机械臂信息
     */
    @Override
    public Boolean insertByBo(SysMechanicalArmInfoBo bo) {
        SysMechanicalArmInfo add = BeanUtil.toBean(bo, SysMechanicalArmInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改机械臂信息
     */
    @Override
    public Boolean updateByBo(SysMechanicalArmInfoBo bo) {
        SysMechanicalArmInfo update = BeanUtil.toBean(bo, SysMechanicalArmInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysMechanicalArmInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除机械臂信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
